/**
 * 第三方库统一配置入口
 * PLM项目专用
 */

// 设置全局process对象，MobX依赖此对象进行环境检测
if (typeof global.process === 'undefined') {
  global.process = {
    env: {
      NODE_ENV: wx.getAccountInfoSync ? 
                (wx.getAccountInfoSync().miniProgram.envVersion === 'release' ? 'production' : 'development') : 
                'development'
    }
  };
} else if (typeof global.process.env === 'undefined') {
  // 确保process.env存在
  global.process.env = {
    NODE_ENV: wx.getAccountInfoSync ? 
              (wx.getAccountInfoSync().miniProgram.envVersion === 'release' ? 'production' : 'development') : 
              'development'
  };
}

// 尝试加载第三方库
let mobx, mobxMiniprogramBindings, dayjs, underscore;

try {
  mobx = require('mobx-miniprogram');
} catch (e) {
  console.error('加载mobx失败:', e);
  // 提供空实现
  mobx = {
    observable: obj => obj,
    action: fn => fn,
    computed: () => null,
    runInAction: fn => fn()
  };
}

try {
  mobxMiniprogramBindings = require('mobx-miniprogram-bindings');
} catch (e) {
  console.error('加载mobx-miniprogram-bindings失败:', e);
  // 提供空实现
  mobxMiniprogramBindings = {
    createStoreBindings: (context, options) => {
      console.log('使用模拟的createStoreBindings');
      const store = options.store || {};
      const fields = options.fields || [];
      const actions = options.actions || [];
      
      // 为组件添加字段绑定
      fields.forEach(field => {
        if (store[field] !== undefined) {
          context.data = context.data || {};
          context.data[field] = store[field];
        }
      });
      
      // 为组件添加动作绑定
      actions.forEach(action => {
        if (typeof store[action] === 'function') {
          context[action] = (...args) => store[action](...args);
        }
      });
      
      // 返回销毁函数
      return {
        destroyStoreBindings: () => {
          console.log('销毁Store绑定');
        }
      };
    }
  };
}

try {
  dayjs = require('dayjs');
} catch (e) {
  console.error('加载dayjs失败:', e);
  // 提供最小实现
  dayjs = date => {
    const d = date ? new Date(date) : new Date();
    return {
      format: (fmt = 'YYYY-MM-DD') => {
        const year = d.getFullYear();
        const month = (d.getMonth() + 1).toString().padStart(2, '0');
        const day = d.getDate().toString().padStart(2, '0');
        const hours = d.getHours().toString().padStart(2, '0');
        const minutes = d.getMinutes().toString().padStart(2, '0');
        const seconds = d.getSeconds().toString().padStart(2, '0');
        
        return fmt
          .replace('YYYY', year)
          .replace('MM', month)
          .replace('DD', day)
          .replace('HH', hours)
          .replace('mm', minutes)
          .replace('ss', seconds);
      }
    };
  };
}

try {
  underscore = require('underscore');
} catch (e) {
  console.error('加载underscore失败:', e);
  // 提供空实现
  underscore = {
    isArray: Array.isArray,
    isObject: obj => obj !== null && typeof obj === 'object',
    isString: str => typeof str === 'string',
    isNumber: num => typeof num === 'number',
    isEmpty: val => val == null || val.length === 0,
    each: (obj, fn) => { 
      if (Array.isArray(obj)) obj.forEach(fn);
      else if (obj && typeof obj === 'object') Object.keys(obj).forEach(key => fn(obj[key], key));
    }
  };
}

// 使用简写变量导出
const _ = underscore;

// 导出模块
module.exports = {
  mobx,
  mobxMiniprogramBindings,
  dayjs,
  underscore,
  _
}; 